﻿using amuudp.package;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace amuudp.sender
{
    public class ImageSender : Sender
    {
        private int clientId;
        private int imageId;
        public byte[] ImageBytes;

        public ImageSender(int clientId, int imageId, Bitmap bitmap, IPEndPoint iPEndPoint) : base(iPEndPoint, null)
        {
            this.clientId = clientId;
            this.imageId = imageId;
            using (MemoryStream stream = new MemoryStream())
            {
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                ImageBytes = new byte[stream.Length];
                stream.Seek(0, SeekOrigin.Begin);
                stream.Read(ImageBytes, 0, Convert.ToInt32(stream.Length));
            }
            createDataBytes();
        }

        public ImageSender(int clientId, int imageId, byte[] imageBytes, IPEndPoint iPEndPoint) : base(iPEndPoint, null)
        {
            this.clientId = clientId;
            this.imageId = imageId;
            ImageBytes = imageBytes;
            createDataBytes();
        }

        private void createDataBytes()
        {
            data = new byte[24 + ImageBytes.Length];
            byte[] clientIdBytes = BitConverter.GetBytes(clientId);
            byte[] typeBytes = BitConverter.GetBytes(PackageType.Image);
            byte[] imageIdBytes = BitConverter.GetBytes(imageId);
            Array.Copy(clientIdBytes, 0, data, 0, 4);
            Array.Copy(typeBytes, 0, data, 8, 4);
            Array.Copy(imageIdBytes, 0, data, 16, 4);
            Array.Copy(ImageBytes, 0, data, 24, ImageBytes.Length);
        }
    }

}
